package hao.bk.searchMap;

import java.sql.SQLException;
import java.util.ArrayList;

import hao.bk.royalmap.GeneralAnimationUtils;
import hao.bk.royalmap.RoyalMapActivity;
import hao.bk.royalmap.SearchBarLayout;
import hao.bk.royalmap.R;
import hao.bk.sql.bo.Typeshop;
import hao.bk.sql.dao.TypeShopDAO;
import hao.bk.sql.sqlutils.SQLiteHelper;
import hao.bk.utils.CharactersMark;
import android.content.Context;
import android.database.Cursor;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class LocalTypeshopLayout extends LinearLayout implements
		OnItemClickListener,TextWatcher {
	private RoyalMapActivity main;
	/** Danh sách tất cả các typeshop lưu trong database */
	private ArrayList<Typeshop> typeshopList;
	private ListView listView;
	protected SearchMapGeneralLayout generalLayout;
	private String preSeachedText;
	protected SearchBarLayout searchBarLayout;
	private TextView alertView;

	public LocalTypeshopLayout (Context context) {
		super(context);
		initParams();
	}
	/**
	 * Khởi tạo các tham số
	 */
	private void initParams() {
		main = RoyalMapActivity.getInstance();
		this.generalLayout = main.searchMap.generalLayout;
		searchBarLayout = generalLayout.searchBarLayout;
		inflate(main,R.layout.local_typeshop_layout, this);
		listView = (ListView) findViewById(R.id.local_list_typeshop);
		listView.setOnItemClickListener(this);
		preSeachedText = "";
		alertView = (TextView) findViewById(R.id.searchmap_typeshop_alert);
		
		/** Vấn đề là ở đây, chưa khởi tạo DB, quá hay */
		SQLiteHelper sqLiteHelper = new SQLiteHelper(main);
		sqLiteHelper.createDataBase();
	}
	/**
	 * Cập nhật danh sách của typeshop
	 */
	public void fillTypeshops() {
		// gán thông tin controll cho typeshop
		generalLayout.searchingAreaId = SearchMapGeneralLayout.SEARCHED_TYPESHOP_STATE;
		
		// lấy danh sách typeshop từ database
		TypeShopDAO typeshopDAO = new TypeShopDAO(main);
		typeshopList = typeshopDAO.getTypeshopList();
		
		// thiết lập TextWatch cho hộp tìm kiếm
		searchBarLayout.textEdt.addTextChangedListener(this);

		searchBarLayout.textEdt.setText("");			
		searchBarLayout.textEdt.setHint(main.getResources().getString(
			R.string.search_typeshop_hit));
		main.setOnBackPressedInterface(generalLayout);
		//pdateSearchTypeshop();  //test
		//ArrayList<Typeshop> typeshopListShow = new ArrayList<Typeshop>();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View arg1, int position,
			long arg3) {
		// ẩn bàn phím
		main.hiddenKeyboardForView(searchBarLayout.textEdt);
		searchBarLayout.textEdt.clearTextChangedListeners();
		
		Typeshop typeshop = (Typeshop) parent.getItemAtPosition(position);
		generalLayout.updateTypeshopInfo(typeshop);

		//Thay đổi nội dung search
		generalLayout.localShopLayout = new LocalShopLayout(main,typeshop);
		GeneralAnimationUtils.startLeftToRight(this, generalLayout.localShopLayout);
		main.searchMap.addContent(generalLayout.localShopLayout);
		generalLayout.localShopLayout.updateShopListShow(typeshop);
	
	}
	/**
	 * @author haoDV Cập nhật danh sách tìm kiếm typeshop
	 */

	public void updateSearchTypeshop() {
		preSeachedText = searchBarLayout.getText();
		String foundStr = CharactersMark.convertFoundString(
				preSeachedText.trim()).toLowerCase();
		ArrayList<Typeshop> typeshopListShow = new ArrayList<Typeshop>();
		String temp = "";
		if (foundStr != null && !foundStr.equals("")) {
			searchBarLayout.showClearBtn(true);
			for (int i = 0; i < typeshopList.size(); i++) {
				Typeshop typeshop = typeshopList.get(i);
				temp = typeshop.getName();
				String inputStr = CharactersMark.convertFoundString(temp)
						.toLowerCase();
				if (inputStr.contains(foundStr)) {
					typeshopListShow.add(typeshop);
				}
			}
		} else {
			typeshopListShow = typeshopList;
			searchBarLayout.showClearBtn(false);
		}
		if(typeshopListShow.isEmpty()){
			listView.setVisibility(GONE);
			alertView.setVisibility(VISIBLE);
		}else{
			listView.setVisibility(VISIBLE);
			alertView.setVisibility(GONE);
			listView.setAdapter(new LocalTypeshopAdapter(main,
				R.id.local_list_typeshop, typeshopListShow));
		}
	}

	@Override
	public void afterTextChanged(Editable arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		updateSearchTypeshop();
	}
}

